package com.woniu.p2p.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.p2p.entity.User;
import com.woniu.p2p.entity.UserCharge;
import com.woniu.p2p.service.P2PChargeService;
import com.woniu.p2p.service.P2PUserService;
import com.woniu.p2p.vo.DeleteChargeVo;
import com.woniu.p2p.vo.FindChargeByConsVo;
import com.woniu.p2p.vo.ShowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 86152
 * @create 2020/8/6
 * @since 1.0.0
 */
@RestController
@RequestMapping("/user")
public class P2PUserController {

    @Autowired
    P2PUserService p2PUserService;

    //登陆
    @RequestMapping("/checklogin")
    public User checkLogin(@RequestBody User requser){
        String username=requser.getUserAccount();
        String password=requser.getUserPassword();
        if(username==null||username==""||password==null||password==""){
            return null;
        }else{
            User user=p2PUserService.findUserByName(username);
            if(user==null){
                return null;
            }else if(password.equals(user.getUserPassword())){
                return user;
            }else{
                return null;
            }
        }
    }

    //获取验证码
    @RequestMapping("/getvercode")
    public Integer getvercode(@RequestBody User user){
        System.out.println(user.getUserPhone());
        String userPhone = user.getUserPhone();
        int vercode = (int) ((Math.random() * 9 + 1) * 100000);
        return vercode;
    }

    //检测用户名是否已经存在
    @RequestMapping("/checkuname")
    public Integer checkuname(@RequestBody User user){
        User userByName = p2PUserService.findUserByName(user.getUserAccount());
        if(userByName==null){
            return 0;
        }else{
            return 1;
        }
    }

    //检测手机号码是否已经注册
    @RequestMapping("/findmobile")
    public Integer checkmobile(@RequestBody User user){
        User userByPhone=p2PUserService.findMobile(user.getUserPhone());
        if(userByPhone==null){
            return 0;
        }else{
            return 1;
        }
    }

    //检测邀请码是否存在
    @RequestMapping("/checkinvcode")
    public Integer checkuserCode(@RequestBody User user){
        User userByUserCode=p2PUserService.findUserCode(user.getUserCode());
        if(userByUserCode==null){
            return 0;
        }else{
            return 1;
        }
    }

    //注册
    @RequestMapping("/register")
    public Integer register(@RequestBody User user){
        int row = p2PUserService.addUser(user);
        return row;
    }


    //登出
    @RequestMapping("/loginout")
    public Integer loginout(HttpServletRequest request){
        String token = request.getHeader("token");
        //删除后端的token

        return null;
    }

    @Autowired
    P2PChargeService p2PChargeService;

    /*查询充值记录*/
    @RequestMapping("/findcharge")
    public PageInfo<UserCharge> findchargelist(HttpServletRequest request){
        String token=request.getHeader("token");
        int userid = Integer.parseInt(token);
        int start=1;
        int size=4;
        PageInfo<UserCharge> userChargePageInfo= p2PChargeService.findAllCharge("",start,size,userid);
        System.out.println(userChargePageInfo);
        return userChargePageInfo;
    }

    /*根据页码查询充值记录*/
    @RequestMapping("/findSinglechargePage")
    public PageInfo<UserCharge> findSinglechargePage(@RequestBody ShowVo showVo,HttpServletRequest request){
        String token=request.getHeader("token");
        int userid = Integer.parseInt(token);
        int start=showVo.getPageNum();
        int size=4;
        PageInfo<UserCharge> userChargePageInfo= p2PChargeService.findAllCharge("",start,size,userid);
        return userChargePageInfo;
    }

    /*条件查询充值记录*/
    @RequestMapping("/findchargeBycons")
    public PageInfo<UserCharge> findchargeBycons(@RequestBody FindChargeByConsVo findChargeByConsVo,HttpServletRequest request) throws ParseException {
        String token=request.getHeader("token");
        int userid = Integer.parseInt(token);
        String chargestatus = findChargeByConsVo.getChargestatus();
        Date starttime=null;
        Date endtime=null;
        if(findChargeByConsVo.getChargetime()!=null){
            String starttime_s = findChargeByConsVo.getChargetime()[0];
            String endtime_s = findChargeByConsVo.getChargetime()[1];
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            starttime = simpleDateFormat.parse(starttime_s);
            endtime = simpleDateFormat.parse(endtime_s);
        }
        Integer start = findChargeByConsVo.getStartPage();
        if(start==null||"".equals(start)){
            start=1;
        }
        int size=4;
        PageInfo<UserCharge> userChargePageInfo=p2PChargeService.selectChargesByCons(chargestatus,starttime,endtime,start,size,userid);
        return userChargePageInfo;
    }

    /*删除充值记录通过ID*/
    @RequestMapping("/deletechargeById")
    public PageInfo<UserCharge> deletechargeById(@RequestBody DeleteChargeVo deleteChargeVo,HttpServletRequest request) throws ParseException {
        String token=request.getHeader("token");
        int userid = Integer.parseInt(token);
        Integer chargeId = deleteChargeVo.getChargeId();
        System.out.println(chargeId);
        String chargestatus = deleteChargeVo.getChargestatus();
        Date starttime=null;
        Date endtime=null;
        if(deleteChargeVo.getChargetime()!=null){
            String starttime_s = deleteChargeVo.getChargetime()[0];
            String endtime_s = deleteChargeVo.getChargetime()[1];
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            starttime = simpleDateFormat.parse(starttime_s);
            endtime = simpleDateFormat.parse(endtime_s);
        }
        Integer start = 1;
        int size=4;
        PageInfo<UserCharge> userChargePageInfo=p2PChargeService.deletechargeById(chargeId,chargestatus,starttime,endtime,start,size,userid);
        return userChargePageInfo;
    }
}
